home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1996 #3 / AmigaPlus_CD-ROM-EXTRA_Nr.3.bin / aminet-spiele / role on / larn / tputs.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  6KB  |  221 lines

  1. /************************************************************************
  2.  *                                  *
  3.  *          Copyright (c) 1982, Fred Fish           *
  4.  *              All Rights Reserved             *
  5.  *                                  *
  6.  *  This software and/or documentation is released for public   *
  7.  *  distribution for personal, non-commercial use only.     *
  8.  *  Limited rights to use, modify, and redistribute are hereby  *
  9.  *  granted for non-commercial purposes, provided that all      *
  10.  *  copyright notices remain intact and all changes are clearly *
  11.  *  documented.  The author makes no warranty of any kind with  *
  12.  *  respect to this product and explicitly disclaims any implied    *
  13.  *  warranties of merchantability or fitness for any particular *
  14.  *  purpose.                            *
  15.  *                                  *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *  tputs     output string with appropriate padding
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *  termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *  tputs(cp,affcnt,outc)
  32.  *  char *cp;
  33.  *  int affcnt;
  34.  *  int (*outc)();
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *  Outputs string pointed to by cp, using function outc, and
  39.  *  following it with the appropriate number of padding characters.
  40.  *  Affcnt contains the number of lines affected, which is used
  41.  *  as a multiplier for the specified per line pad time.  If
  42.  *  per line pad count is not applicable, affcnt should be 1,
  43.  *  NOT zero.
  44.  *
  45.  *  The format of the string pointed to by cp is:
  46.  *
  47.  *      [pad time][*]<string to send>
  48.  *
  49.  *      where:  pad time => time to delay in milliseconds
  50.  *          * => specifies that time is per line
  51.  *          
  52.  *  The pad character is assumed to reside in the external
  53.  *  variable "PC".  Also, the external variable "ospeed"
  54.  *  should contain the output speed of the terminal as
  55.  *  encoded in /usr/include/sgtty.h  (B0-B9600).
  56.  *
  57.  *  BUGS
  58.  *
  59.  *  Digit conversion is based on native character set
  60.  *  being ASCII.
  61.  *
  62.  */
  63.  
  64. /*
  65.  *  Miscellaneous stuff
  66.  */
  67.  
  68. #include <stdio.h>
  69. #include <ctype.h>
  70.  
  71. # ifndef MSDOS
  72. extern char PC;         /* Pad character to use */
  73. extern char ospeed;     /* Encoding of output speed */
  74.  
  75. static int times[] = {
  76.     0,              /* Tenths of ms per char 0 baud */
  77.     2000,           /* Tenths of ms per char 50 baud */
  78.     1333,           /* Tenths of ms per char 75 baud */
  79.     909,            /* Tenths of ms per char 110 baud */
  80.     743,            /* Tenths of ms per char 134 baud */
  81.     666,            /* Tenths of ms per char 150 baud */
  82.     500,            /* Tenths of ms per char 200 baud */
  83.     333,            /* Tenths of ms per char 300 baud */
  84.     166,            /* Tenths of ms per char 600 baud */
  85.     83,             /* Tenths of ms per char 1200 baud */
  86.     55,             /* Tenths of ms per char 1800 baud */
  87.     41,             /* Tenths of ms per char 2400 baud */
  88.     20,             /* Tenths of ms per char 4800 baud */
  89.     10              /* Tenths of ms per char 9600 baud */
  90. };
  91. # endif
  92.  
  93.  
  94. /*
  95.  *  PSEUDO CODE
  96.  *
  97.  *  Begin tgoto
  98.  *      If string pointer is invalid then
  99.  *      Return without doing anything.
  100.  *      Else
  101.  *      For each pad digit (if any)
  102.  *          Do decimal left shift.
  103.  *          Accumulate the lower digit.
  104.  *      End for
  105.  *      Adjust scale to tenths of milliseconds
  106.  *      If there is a fractional field
  107.  *          Skip the decimal point.
  108.  *          If there is a valid tenths digit
  109.  *          Accumulate the tenths.
  110.  *          End if
  111.  *          Discard remaining digits.
  112.  *      End if
  113.  *      If per line is specified then
  114.  *          Adjust the pad time.
  115.  *          Discard the per line flag char.
  116.  *      End if
  117.  *      While there are any characters left
  118.  *          Send them out via output function.
  119.  *      End while
  120.  *      Transmit any padding required.
  121.  *      End if
  122.  *  End tgoto
  123.  *
  124.  */
  125.  
  126. tputs(cp,affcnt,outc)
  127. char *cp;
  128. int affcnt;
  129. int (*outc)();
  130. {
  131.     int ptime;          /* Pad time in tenths of milliseconds */
  132.  
  133.     if (cp == NULL || *cp == NULL) {
  134.     return;
  135.     } else {
  136.     for (ptime = 0; isdigit(*cp); cp++) {
  137.         ptime *= 10;
  138.         ptime += (*cp - '0');
  139.     }
  140.     ptime *= 10;
  141.     if (*cp == '.') {
  142.         cp++;
  143.         if (isdigit(*cp)) {
  144.         ptime += (*cp++ - '0');
  145.         }
  146.         while (isdigit(*cp)) {cp++;}
  147.     }
  148.     if (*cp == '*') {
  149.         ptime *= affcnt;
  150.         cp++;
  151.     }
  152.     while (*cp != NULL) {
  153.         (*outc)(*cp++);
  154.     }
  155. # ifndef MSDOS
  156. # ifndef VMS
  157.     do_padding(ptime,outc);
  158. # endif
  159. # endif
  160.     }
  161. }
  162.  
  163. # ifndef MSDOS
  164. /*
  165.  *  FUNCTION
  166.  *
  167.  *  do_padding    transmit any pad characters required
  168.  *
  169.  *  SYNOPSIS
  170.  *
  171.  *  static do_padding(ptime,outc)
  172.  *  int ptime;
  173.  *  int (*outc)();
  174.  *
  175.  *  DESCRIPTION
  176.  *
  177.  *  Does any padding required as specified by ptime (in tenths
  178.  *  of milliseconds), the output speed given in the external
  179.  *  variable ospeed, and the pad character given in the
  180.  *  external variable PC.
  181.  *
  182.  */
  183.  
  184. /*
  185.  *  PSEUDO CODE
  186.  *
  187.  *  Begin do_padding
  188.  *      If there is a non-zero pad time then
  189.  *      If the external speed is in range then
  190.  *          Look up the delay per pad character.
  191.  *          Round pad time up by half a character.
  192.  *          Compute number of characters to send.
  193.  *          For each pad character to send
  194.  *          Transmit the pad character.
  195.  *          End for
  196.  *      End if
  197.  *      End if
  198.  *  End do_padding
  199.  *
  200.  */
  201.  
  202. static do_padding(ptime,outc)
  203. int ptime;
  204. int (*outc)();
  205. {
  206.     register int nchars;
  207.     register int tpc;
  208.  
  209.     if (ptime != 0) {
  210.     if (ospeed > 0 && ospeed <= (sizeof(times)/ sizeof(int))) {
  211.         tpc = times[ospeed];
  212.         ptime += (tpc / 2);
  213.         nchars = ptime / tpc;
  214.         for ( ; nchars > 0; --nchars) {
  215.         (*outc)(PC);
  216.         }
  217.     }
  218.     }
  219. }
  220. # endif
  221.